package com.beiming.libra.controller;

import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.domain.ArbitrationCaseAdjust;
import com.beiming.libra.domain.ArbitrationCaseAdjustMessages;
import com.beiming.libra.domain.dto.responsedto.CaseAdjustResponseDTO;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.ArbitrationCaseAdjustService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: tyrion
 * @Date: 2018/6/7 17:21
 * @类描述: 调解控制层
 */
@RestController
@RequestMapping("/api/adjust")
@Api(value = "调解处理接口", tags = "调解处理接口")
public class ArbitrationCaseAdjustController {

  @Autowired
  ArbitrationCaseAdjustService arbitrationCaseAdjustService;

  @GetMapping(value = "/listAllParties/{caseId}")
  @ApiOperation(value = "查询所有有关当事人的信息", notes = "查询所有有关当事人的信息")
  public Result listAllParties(
      @ApiParam(value = "案件id", required = true) @PathVariable("caseId") String caseId) {
    List<CaseAdjustResponseDTO> listAllParties = arbitrationCaseAdjustService
        .listAllParties(caseId);

    if (listAllParties.size() == 0) {
      return Result.failed(ErrorCode.RESULT_EMPTY, "该案件无当事人");
    }
    return Result.success(listAllParties);
  }


  @PostMapping(value = "/applyOrRejectAdjust")
  @ApiOperation(value = "申请或撤销调解", notes = "申请调解,传入id,caseId,applyStatus,设置applyStatus为0表示撤销，设置为1表示申请")
  public Result applyOrRejectAdjust(ArbitrationCaseAdjust aca) {
    arbitrationCaseAdjustService.insertOrUpdateStatus(aca);
    String caseId = aca.getCaseId();
    if ("1".equals(aca.getApplyStatus())) { //申请
      //通知办案秘书所有人都已经提出申请
      arbitrationCaseAdjustService.notifySecreIfAllSend(caseId);
    }
    return Result.success("处理成功");
  }


  @GetMapping(value = "/listLeaveMessages/{caseId}")
  @ApiOperation(value = "获取当前案件留言信息列表", notes = "根据案件id获取当前案件留言信息列表")
  public Result listLeaveMessages(
      @ApiParam(value = "案件id", required = true) @PathVariable("caseId") String caseId) {
    List<ArbitrationCaseAdjustMessages> listLeaveMessages = arbitrationCaseAdjustService
        .listLeaveMessages(caseId);
    if (listLeaveMessages.size() == 0) {
      return Result.failed(ErrorCode.RESULT_EMPTY, "该案件暂无留言信息");
    }
    return Result.success(listLeaveMessages);
  }

  /**
   * 保存留言信息
   * @param acam
   * @return
   */
  @PostMapping(value = "/saveLeaveMessage")
  @ApiOperation(value = "保存留言信息", notes = "保存留言信息，需必传入userId和caseId和userType")
  public Result insertLeaveMessage(ArbitrationCaseAdjustMessages acam) {
    arbitrationCaseAdjustService.insertLeaveMessage(acam);
    return Result.success();
  }

}
